package com.touchapk.javafx;

import java.io.File;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;



/**
 * @description
 * @author yangkeyun
 * @email coyun@qq.com
 * @date 2016年8月7日
 * @version V1.0
 */
public class NewController implements DialogController {

	private MainController mainController;

	public NewController() {
	}

	public NewController(MainController mainController) {
		this.mainController = mainController;
	}

	private String path;
	
	
	/**
	 * @return the path
	 */
	public String getPath() {
		return path;
	}

	/**
	 * @param path the path to set
	 */
	public void setPath(String path) {
		this.path = path;
	}

	private FXMLDialog dialog;

	@Override
	public void setDialog(FXMLDialog dialog) {
		this.dialog = dialog;
	}
	private String msg;
	
	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	@FXML
	private Label label;
	@FXML
	private TextField textField;
	@FXML
	private Button okButton;
	
	@FXML
	public void okFunc() {
		
		String name=textField.getText();
		if("".equals(name.trim())){
			return;
		}
		
		this.mainController.runCall(name);
		
		this.dialog.close();
		
	}

	
	@FXML
	public void cancelFunc() {
		this.dialog.close();
	}

	/**
	 * Initializes the controller class.
	 */
	@FXML
	// This method is called by the FXMLLoader when initialization is complete
	void initialize() {
		
		if(!"".equals(this.msg)){
			label.setText(this.msg);
		}
		
		textField.textProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue observable, Object oldValue, Object newValue) {
				String nPath=path+"\\"+newValue.toString();
				File nFile=new File(nPath);
				if(nFile.exists()){
					okButton.setDisable(true);
				}else{
					okButton.setDisable(false);
				}
			}
		});
	}
	


}
